package m.base.action;

import m.base.model.LevelInfo;
import m.base.service.LevelInfoService;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.*;
import m.system.cache.CacheUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "base/levelInfo")
public class LevelInfoAction extends BaseStatusAction {
    private LevelInfo model;
    public JSONMessage getAllLevel(){
        return new JSONMessage(0)
            .push("allLevel", CacheUtil.getList(LevelInfo.class,""));
    }
    /**
     * 保存
     */
    @RequestMeta(logTitle = "基础数据",logDescription = "保存商户级别信息")
    public JSONMessage doSave() throws Exception {
        PowerUtil.verifyPower(getStatusPower());
        String msg=getService(LevelInfoService.class).save(model);
        return new JSONMessage(0,msg)
                .push("model", model);
    }
    /*
     * 编辑级别信息
     */
    public VueResult toEdit() throws Exception {
        PowerUtil.verifyPower(getStatusPower());//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*");
        }else{
            model=new LevelInfo();
            model.setUserNum(1);
            model.setRepositoryNum(1);
            model.setMonthOldAmount(0.0);
            model.setMonthAmount(0.0);
            model.setYearAmount(0.0);
            model.setOfferFunc(LevelInfo.No);
            model.setCollectFunc(LevelInfo.No);
            model.setExportFunc(LevelInfo.No);
            model.setChartFunc(LevelInfo.No);
            model.setDefLevel(LevelInfo.No);
            model.setApplyLevel(LevelInfo.No);
        }
        ModelForm form = new ModelForm("商户级别",3);
        form.addItem(
            FormInput.switchBox("默认","model.defLevel",LevelInfo.Yes,LevelInfo.No),
            FormInput.input("名称", "model.name").setPlaceholder("请输入名称"),
            FormInput.number("排序", "model.sort","",0).setPlaceholder("请输入排序"),
            FormInput.switchBox("注册","model.applyLevel",LevelInfo.Yes,LevelInfo.No),
            FormHtml.ins("颜色",
                Element.tag("a-tag").A("v-for","item in dictOptions('color')")
                .A(":color","model.color==item.value?item.value:item.label")
                .A("@click","model.color=item.value").E("{{model.name||'颜色'}}")
            ).setSpan(2),
            FormInput.number("用户数量", "model.userNum","",0).setPlaceholder("请输入用户数量"),
            FormInput.radio("导出功能", "model.exportFunc", "m.base.model.LevelInfo!exportFunc"),
            FormInput.radio("报账单功能", "model.offerFunc", "m.base.model.LevelInfo!offerFunc"),
            FormInput.number("仓库数量", "model.repositoryNum","",0).setPlaceholder("请输入仓库数量"),
            FormInput.radio("图表功能", "model.chartFunc", "m.base.model.LevelInfo!chartFunc"),
            FormInput.radio("统计汇总功能", "model.collectFunc", "m.base.model.LevelInfo!collectFunc"),
            FormInput.number("原月功能费", "model.monthOldAmount").setPlaceholder("请输入原月功能费"),
            FormInput.number("月功能费", "model.monthAmount").setPlaceholder("请输入月功能费"),
            FormInput.number("年功能费", "model.yearAmount").setPlaceholder("请输入年功能费")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    /*
     * 级别信息列表
     */
    public VueResult allList() throws Exception {
        boolean power=PowerUtil.hasPower(getStatusPower());
        VueListResult result=getVueListResult("商户级别列表",LevelInfo.class,actionUrl("allList"));
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addQueryFields("name","color");
        result.addCol(toTextColumn("默认", "defLevel", 100, true).setDictType("m.base.model.LevelInfo!defLevel"));
        result.addCol(toTextColumn("注册", "applyLevel", 100, true).setDictType("m.base.model.LevelInfo!applyLevel"));
        result.addCol(toElementColumn("名称", new ListElement("a-tag")
            .addAttr(":color","record.color")
            .addText("{{record.name}}")
        ).setAlign(Align.CENTER));
        result.addCol(toIntegerColumn("排序", "sort", 100, true).setSort(ListSortOrder.ASC).setCount(ListCount.NONE));
        result.addCol(toNumberColumn("原月功能费", "monthOldAmount", 100, true));
        result.addCol(toNumberColumn("月功能费", "monthAmount", 100, true));
        result.addCol(toNumberColumn("年功能费", "yearAmount", 100, true));
        result.addCol(toTextColumn("用户数量", "userNum", 100, true));
        result.addCol(toTextColumn("仓库数量", "repositoryNum", 100, true));
        result.addCol(toTextColumn("报价单", "offerFunc", 100, true).setDictType("m.base.model.LevelInfo!offerFunc"));
        result.addCol(toTextColumn("统计汇总", "collectFunc", 100, true).setDictType("m.base.model.LevelInfo!collectFunc"));
        result.addCol(toTextColumn("导出功能", "exportFunc", 100, true).setDictType("m.base.model.LevelInfo!exportFunc"));
        result.addCol(toTextColumn("图表功能", "chartFunc", 100, true).setDictType("m.base.model.LevelInfo!chartFunc"));
        result.addCol(toStatusColumn(power, true).setDictType("m.base.model.LevelInfo!status"));
        result.addCol(toSpaceColumn("操作", 100,
            ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),900, SuccessMethod.REFRESH)
                .addParam("model.oid","oid")
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),900, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }
    @Override
    public LevelInfo getModel() {
        return model;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{PowerUtil.SysData};
    }

    public void setModel(LevelInfo model) {
        this.model = model;
    }
}
